package com.jsnkj.basic.dialog

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.jsnkj.basic.R
import com.jsnkj.basic.bean.ReadPacketMessageBean
import com.jsnkj.basic.config.RewardsConfig
import com.jsnkj.basic.util.RewardsUtil
import com.jsnkj.basic.util.SharePreferencesUtil
import com.bumptech.glide.Glide
import com.jsnkj.basic.util.DoubleClickHelper1

//import com.tb.mob.bean.Position


/**
 * 打开红包弹窗
 */
class OpenRedPacketDialog(
    var dialogContext: Activity,
    var bean: ReadPacketMessageBean,
    var click: onItemClickListener
) :
    Dialog(dialogContext), View.OnClickListener {

    var tvMoney: TextView? = null
    var frameLayoutAd: FrameLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_open_red_packet)
        setCanceledOnTouchOutside(false)
        setCancelable(true)
        window?.setBackgroundDrawableResource(android.R.color.transparent)

        val imgClose: ImageView = findViewById(R.id.img_close)
        imgClose.setOnClickListener(this)

        val tvOpen: TextView = findViewById(R.id.tv_open)
        DoubleClickHelper1.click(tvOpen) {
            com.jsnkj.basic.util.LogUtils.e("红包弹窗 -- 领取奖励")
            dismiss()
            click.openItemClickListener()
        }


        val imgIcon: ImageView = findViewById(R.id.img_icon)
        Glide.with(dialogContext).load(bean.avatar).into(imgIcon)

        val tvUser: TextView = findViewById(R.id.tv_user)
        tvUser.text = "${bean.userName}发出的红包"

        val tvGreeting: TextView = findViewById(R.id.tv_greeting)
        tvGreeting.text = "恭喜发财，财源广进"

        tvMoney = findViewById(R.id.tv_money)
        frameLayoutAd = findViewById(R.id.fl_ad)

        //加载信息流 todo
//        AdvertisementManager.loadFeed(
//            dialogContext, 300, 169,
//            object : com.tb.mob.TbManager.FeedLoadListener {
//                override fun onFail(p0: String?) {
//                    BurialPointUtil.flowAdEvent("fail")
//                }
//
//                override fun onLoad(feedPosition: FeedPosition) {
//                    BurialPointUtil.flowAdEvent("show");
//                    feedPosition.showFeed(dialogContext, frameLayoutAd)
//                }
//
//                override fun onDismiss() {
//
//                }
//
//                override fun onClicked() {
//
//                }
//
//                override fun onExposure(position: Position?) {
//                    position?.let {
//                        RewardsUtil.addGoldNumber(it.ecpm, RewardsConfig.adStream)
//                    }
//                }
//
//                override fun onVideoReady() {
//
//                }
//
//                override fun onVideoComplete() {
//
//                }
//
//            })
    }


    fun showDialog() {
        this.show()

        if (null == tvMoney) {
            tvMoney = findViewById(R.id.tv_money)
        }

        RewardsUtil.addAdTypeRewardGoldNumber(RewardsConfig.adStream)
        val maxRedpackAmount =
            SharePreferencesUtil.getInstance().getSettingEntity().max_redpack_amount

        var money = 0
        RewardsConfig.beanList = ArrayList()
        for ((_, bean) in SharePreferencesUtil.getInstance().getAdPostTypeBean().list.withIndex()) {
            com.jsnkj.basic.util.LogUtils.i("当前加载数据：$bean")
            if (money < maxRedpackAmount) {
                money += bean.amount!!.toInt()

                RewardsConfig.beanList.add(bean)
            } else {
                break
            }
        }

        RewardsConfig.goldMoney = money

        if (RewardsConfig.goldMoney > maxRedpackAmount) {
            RewardsConfig.goldMoney = maxRedpackAmount
        }

        com.jsnkj.basic.util.LogUtils.i("当前金币数量：${RewardsConfig.goldMoney}   maxRedpackAmount == $maxRedpackAmount")

        val spannableString = SpannableString("${RewardsConfig.goldMoney}金币")

        // 设置“5000”文本的大小
        spannableString.setSpan(
            AbsoluteSizeSpan(35, true),
            0,
            RewardsConfig.goldMoney.toString().length - 1,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        // 设置“金币”文本的大小
        spannableString.setSpan(
            AbsoluteSizeSpan(15, true),
            RewardsConfig.goldMoney.toString().length,
            spannableString.length,
            Spanned.SPAN_INCLUSIVE_INCLUSIVE
        )
        tvMoney?.text = spannableString
    }


    override fun onClick(view: View) {
        dismiss()
        when (view.id) {
            R.id.img_close -> {
                click.closeCallBack()
            }

//            R.id.tv_open -> {
//                click.openItemClickListener()
//            }
        }
    }


    interface onItemClickListener {
        fun openItemClickListener()
        fun closeCallBack()
    }

}